package com.antdesignbackend.backend.webAPP;

import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.TreatmentRecord;
import com.antdesignbackend.backend.entity.entityVo.TreatmentRecordVo;
import com.antdesignbackend.backend.service.TreatmentRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/treatmentRecordAPP")
public class TreatmentRecordAPPController{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    @Autowired
    TreatmentRecordService treatmentRecordService;

    /**
     *诊疗记录
     */
    @GetMapping("/index")
    public String index(Model model) {
//        model.addAttribute("animalTypes",getLoginAnimalTypes());
        return "records/treatmentRecord/index";
    }
    /**
     * 表格查询
     */
    @PostMapping("/tableSearch")
    @ResponseBody
    public ReturnVO tableSearch(@RequestBody TreatmentRecordVo treatmentRecordVo) throws NoSuchMethodException, SecurityException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        List<TreatmentRecordVo> treatmentRecordVos=new ArrayList<>();
        QueryWrapper<TreatmentRecordVo> wrapper =new QueryWrapper<>();
        if(treatmentRecordVo.getLivestockNo()!=null&& !treatmentRecordVo.getLivestockNo().equals(""))
        {
            wrapper.like("a.livestockNo",treatmentRecordVo.getLivestockNo());
        }
        if (treatmentRecordVo.getTimeInterval() != null && !treatmentRecordVo.getTimeInterval().equals("")){
            String[] str=treatmentRecordVo.getTimeInterval().split(" - ");
            String star=str[0];
            String end=str[1];
            wrapper.between("a.treatmentDate",star,end);
        }
        wrapper.orderByDesc("a.treatmentDate").orderByDesc("a.reportDate");
        wrapper.eq("a.eid",treatmentRecordVo.getEId());
        //先统计满足条件的数据总数，因为sql 不包括排序
        int total=treatmentRecordService.count(wrapper);
        result.setTotal(total);
        //再查询
        if(total>=0)
        {
            treatmentRecordVos=treatmentRecordService.search(wrapper,new Page<>(treatmentRecordVo.getPageIndex(),treatmentRecordVo.getPageSize()));
        }
        result.setData(treatmentRecordVos);
        return result;
    }

    /**
     * 根据id删除
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public ReturnVO deleteById(@PathVariable Serializable id) {
        result = new ReturnVO();
        if (treatmentRecordService.removeById(Integer.parseInt(id.toString())) == true) {
            result.setMessage("删除成功");
        } else {
            result.setMessage("删除失败");
            result.setCode("0");
        }
        return result;
    }
    /**
     * 修改/新增操作的保存
     */
    @PostMapping("/save")
    @ResponseBody
    public ReturnVO save(@RequestBody TreatmentRecord treatmentRecord) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        if(treatmentRecord.getTreatmentRecordId()==0){
            treatmentRecord.setUId(treatmentRecord.getUId());
            treatmentRecord.setEId(treatmentRecord.getEId());
            treatmentRecord.setReportDate(new Date());
            b=treatmentRecordService.save(treatmentRecord);
        }else{
            b=treatmentRecordService.updateById(treatmentRecord);
        }
        if(b==true){
            result.setMessage("保存成功！");
            result.setCode("200");
            result.setData(treatmentRecord);
        }
        else
        {
            result.setMessage("保存失败！");
            result.setCode("500");
        }
        return result;
    }
}
